
class ImSingleton {

    static instance = null;

    constructor() {
        this.localSeqCreator = new DefaultSeqCreator();
        this.imCacheSession = new ImCacheSession();
    }

    static getInstance() {
        if (ImSingleton.instance === null) {
            ImSingleton.instance = new ImSingleton();
        }

        return ImSingleton.instance;
    }

    get seqCreator() {
        return this.localSeqCreator;
    }

    get session() {
        return this.imCacheSession;
    }

    loginSuccess(loginResp) {
        this.imCacheSession.userId = loginResp.userId;
        this.imCacheSession.putData('token', loginResp.token);
        this.imCacheSession.putData('authCode', loginResp.authCode);
        this.imCacheSession.putData('authToken', loginResp.authToken);
        this.imCacheSession.putData('tenantCode', loginResp.tenantCode);
    }

    logout() {
        this.imCacheSession.userId = undefined;
        this.imCacheSession.clear();
    }

    isLogin() {
        return this.imCacheSession.userId !== null && this.imCacheSession.has("authCode");
    }
}

class ImCacheSession {
    userId = undefined;
    sessionDataMap = new Map();

    constructor() {
    }

    getData(key){
        return this.sessionDataMap.get(key);
    }

    putData(key, data) {
        this.sessionDataMap.set(key, data);
    }

    has(key) {
        return this.sessionDataMap.has(key);
    }

    clear() {
        this.sessionDataMap.clear();
    }
}